import logging

import pytest

from resource.pom.keyboard import Keyboard
from resource.pom.settings import Settings


class Test:
    app = Settings
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device, factory_reset):
        logging.info('步骤1.点击设置应用')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        logging.info('预期结果1.进入设置页面')
        device.assert_current_window(self.app.window_name)

        logging.info('步骤2.点击生物识别和密码')
        device.click(*self.app.biometrics_password_item)
        logging.info('预期结果2.进入生物识别和密码页面')
        device.refresh_layout()
        device.assert_text_exist('人脸识别')
        device.assert_text_exist('锁屏密码')

        logging.info('步骤3.点击锁屏密码-其他密码类型-混合密码')
        device.click_element(device.get_element_by_text('锁屏密码'))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('其他密码类型'))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('混合密码') )
        logging.info('预期结果3.输入6-32字符，至少包含1个字母，未按照要求给出提示；按照要求则继续再次确认密码，两次密码不一致给出提示')
        device.refresh_layout()
        device.assert_text_exist('密码由6-32字符字组成，需至少包含1个字母')
        device.click_element(device.get_element_by_type('TextInput'))
        device.click(624, 290)
        device.wait(2)
        device.assert_soft_keyboard_on()
        if device.keyboard_mode() != 'numpad':
            device.click(*self.keyboard.abc_123)
        device.click(*self.keyboard.key_1)
        device.click(*self.keyboard.key_2)
        device.click(*self.keyboard.key_3)
        device.click(*self.keyboard.key_4)
        device.click(*self.keyboard.key_5)
        device.click(*self.keyboard.key_6)
        device.refresh_layout()
        device.assert_text_exist('请至少包含 1 个字母')
        device.click(*self.keyboard.delete)
        device.click(*self.keyboard.abc_123)
        device.click(*self.keyboard.key_a)
        device.click(*self.keyboard.close)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('继续'))
        device.refresh_layout()
        device.assert_text_exist('请再次输入密码')
        text_input = device.get_element_by_type('TextInput')
        device.click_element(text_input)
        device.click(624, 290)
        device.wait(2)
        device.assert_soft_keyboard_on()
        text_input_xy = device.center_of_element(text_input)
        device.input_text(*text_input_xy, '123456')
        device.click(*self.keyboard.close)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('确定'))
        device.refresh_layout()
        device.assert_text_exist('请再次输入密码')
        device.assert_type_exist('TextInput')
        device.assert_text_exist('取消')
        device.assert_text_exist('确定')
        device.click_element(text_input)
        device.click(624, 290)
        device.wait(2)
        device.assert_soft_keyboard_on()
        device.input_text(*text_input_xy, '12345a')
        device.click(*self.keyboard.close)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('确定'))
        device.refresh_layout()
        device.assert_text_exist('人脸识别')
        device.assert_text_exist('更改锁屏密码')
        device.assert_text_exist('关闭锁屏密码')

        logging.info('步骤4.修改锁屏密码，进入其他密码类型，点击自定义数字密码')
        device.click_element(device.get_element_by_text('更改锁屏密码'))
        device.refresh_layout()
        device.click_element(device.get_element_by_type('TextInput'))
        device.wait(2)
        device.assert_soft_keyboard_on()
        device.input_text(*device.center_of_element(device.get_element_by_type('TextInput')), '12345a')
        device.click(*self.keyboard.close)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('下一步'))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('其他密码类型'))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('自定义数字密码'))
        logging.info('预期结果4.输入6-32位数字密码点继续，再次确认密码，正确则退回生物识别和密码页面')
        device.refresh_layout()
        device.assert_text_exist('密码由6-32位数字组成')
        device.assert_type_exist('TextInput')
        pwd_area = device.get_element_by_type('TextInput')
        device.click_element(pwd_area)
        device.click(624, 290)
        device.wait(2)
        device.assert_soft_keyboard_on()
        device.input_text(*device.center_of_element(pwd_area), '12345678')
        device.click(*self.keyboard.close)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('继续'))
        device.click_element(pwd_area)
        device.click(624, 290)
        device.wait(2)
        device.assert_soft_keyboard_on()
        device.input_text(*device.center_of_element(pwd_area), '12345678')
        device.click(*self.keyboard.close)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('确定'))
        device.refresh_layout()
        device.assert_text_exist('人脸识别')
        device.assert_text_exist('更改锁屏密码')
        device.assert_text_exist('关闭锁屏密码')
